import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class Crawler {

	public static String baseUrl = "http://www.chon.vn";
	public static int index = 1;

	public static void main(String[] args) throws IOException,
			InterruptedException {
		System.out.println("Begin");

		long Start = System.nanoTime();

		ChonCrawler chonCrawler = new ChonCrawler(baseUrl);
		chonCrawler.crawl();

		System.out.println("End - "
				+ String.valueOf(TimeUnit.NANOSECONDS.toMinutes(System
						.nanoTime() - Start)));

	}

	public static Document getHtml(String url) {
		Document doc = null;
		try {
			Connection connection = Jsoup
					.connect(url)
					.timeout(0)
					.userAgent(
							"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20130406 Firefox/23.0");
			doc = connection.get();
			// System.out.print(doc);
		} catch (Exception e1) {
			System.out.print(e1);
		}

		return doc;
	}

	// public static void getAddressStructure()
	// throws FailingHttpStatusCodeException, MalformedURLException,
	// IOException {
	// String lBaseUrl =
	// "http://www.chon.vn/nguoi-dung/dang-nhap.aspx?ReturnUrl=%2fnguoi-dung%2fquan-ly%2fdefault.aspx";
	//
	// HSSFWorkbook workbook = new HSSFWorkbook();
	// HSSFSheet sheet = workbook.createSheet("Sample sheet");
	// int rownum = 0;
	// Row row = sheet.createRow(rownum++);
	// // OutputStreamWriter writer = new OutputStreamWriter(
	// // new FileOutputStream("D:\\Log.txt"), "UTF-8");
	//
	// WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
	// HtmlPage page = (HtmlPage) webClient.getPage(lBaseUrl);
	// HtmlForm form = page.getFormByName("aspnetForm");
	// List<HtmlElement> elements = form.getElementsByTagName("select");
	//
	// HtmlSelect cities = (HtmlSelect) elements.get(1);
	// HtmlSelect district = (HtmlSelect) elements.get(2);
	// // writer.append(cities.asXml());
	// List<HtmlElement> citylist = cities.getElementsByTagName("option");
	// System.out.println(citylist.size());
	//
	// for (int i = 0; i < citylist.size(); i++) {
	// HtmlOption city = (HtmlOption) citylist.get(i);
	// city.setSelected(true);
	// // writer.append(city.getTextContent());
	// System.out.println(city.getTextContent());
	// webClient.waitForBackgroundJavaScript(3000);
	//
	// page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
	// form = page.getFormByName("aspnetForm");
	// elements = form.getElementsByTagName("select");
	//
	// cities = (HtmlSelect) elements.get(1);
	// citylist = cities.getElementsByTagName("option");
	// district = (HtmlSelect) elements.get(2);
	// city = (HtmlOption) citylist.get(i);
	// for (HtmlElement htmlElement : district
	// .getElementsByTagName("option")) {
	// if (htmlElement.getIndex() == 0)
	// break;
	// Cell cell = row.createCell(0);
	// cell.setCellValue(city.getTextContent());
	// cell = row.createCell(1);
	// cell.setCellValue(htmlElement.getTextContent());
	// row = sheet.createRow(rownum++);
	// }
	// System.out.println(i);
	// }
	//
	// try {
	// FileOutputStream out = new FileOutputStream(new File("D:\\new.xls"));
	// workbook.write(out);
	// out.close();
	// System.out.println("Excel written successfully..");
	//
	// } catch (FileNotFoundException e) {
	// e.printStackTrace();
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	//
	// }
	//
	// public static int parseInt(final String s) {
	// // Check for a sign.
	// int num = 0;
	// int sign = -1;
	// final int len = s.length();
	// final char ch = s.charAt(0);
	// if (ch == '-')
	// sign = 1;
	// else
	// num = '0' - ch;
	//
	// // Build the number.
	// int i = 1;
	// while (i < len)
	// num = num * 10 + '0' - s.charAt(i++);
	//
	// return sign * num;
	// }
}
